clear all
set more off

clear all

local alpha = `1'
local beta = `2'
local sigma = `3'
local sigmax = `4'
local J = `5'
local N = `6'
local dgp = `7'


set obs `N'


if(`dgp' == 1) {
gen c = exp(-2+1.5*rnormal())
}

if(`dgp' == 2) {
*Now c is the threshold utility at which you stop searching
 gen c = exp(-0.35+1.5*rnormal())
}

if(`dgp' == 3) {
*Now you search if free utility > c
 gen c = -1+4*rnormal()
}

if(`dgp' == 4) {
*Randomly search up to c goods
 gen c = `J'
}



gen alpha = `alpha'
gen beta = `beta'
gen ID = _n

do programs/makedatasetup.do `J' `sigma' `sigmax'

if(`dgp' == 1) {
do programs/makesimsetup.do `J' `sigma' `sigmax'
}

if(`dgp' == 2) {
do programs/makesimsetupsatisfice.do `J' `sigma' `sigmax'
}

if(`dgp' == 3) {
do programs/makesimsetupthreshold.do `J' `sigma' `sigmax'
}

if(`dgp' == 4) {
do programs/makesimsetuprandomstop.do `J' `sigma' `sigmax'
}



*find good 1 in each choice set
bys ID: egen maxz = max(z)
gen ismax = (maxz == z)

gen maxID = altID if ismax == 1
bys ID: egen maxmaxID = max(maxID)
drop maxID
rename maxmaxID maxID


*naive logit ratio
clogit chosen x z, group(ID)
local naivelogit = _b[z]/_b[x]


*Create attributes of rival goods
preserve
keep ID altID x z
reshape wide x z, i(ID) j(altID)
tempfile attributes
save `attributes', replace
restore

merge m:1 ID using `attributes'
assert _merge == 3
drop _merge




gen vijnaive = _b[x]*x+_b[z]*z

gen dv1dz1est = _b[z]

local vij = "vijnaive"
local label = "naive"
do programs/makelogitprob.do "ID" "vij" "`label'" "`J'" "altID"

*Adjust weights so that denominator is never too small causing weights to blow up
replace sijnaive = max(sijnaive,.1)
replace sijnaive = min(sijnaive,0.9)

forvalues k = 1/`J' {
gen si`k'naive = sijnaive if altID == `k'
bys ID: egen si`k'maxnaive = max(si`k'naive)
drop si`k'naive
rename si`k'maxnaive si`k'naive
}

forvalues k = 1/`J' {

*Theoretical best
gen bx_`k' = si`k'naive/(1-sijnaive)

gen bz_`k' = si`k'naive/(1-sijnaive)

gen b2x_`k' = dv1dz1est*(1-2*sijnaive)*si`k'naive/((1-sijnaive)*(1+dv1dz1est*(1-2*sijnaive)*z))

gen b2z_`k' = dv1dz1est*(1-2*sijnaive)*si`k'naive/((1-sijnaive)*(1+dv1dz1est*(1-2*sijnaive)*z))

gen b2test_`k' = dv1dz1est*(1-2*sijnaive)*si`k'naive/(1-sijnaive)

*Adjust weights so that denominator is never too small causing weights to blow up

gen newval_`k' = 1+dv1dz1est*(1-2*sijnaive)*z
gen absnew_`k' = abs(newval_`k')
replace b2x_`k' = b2test_`k' if absnew_`k' <= 1
replace b2z_`k' = b2test_`k' if absnew_`k' <= 1
drop newval_`k' absnew_`k'
} 

do programs/makevariablessym.do `J'




*ratio estimate based on SOD
clogit chosen x z zzmax zsum zzsum zxsum xsum, group(ID)

local gammax = _b[xsum]
local gammaz = _b[zsum]
local deltaz = _b[zzsum]
local deltax = _b[zxsum]
local num = -_b[z]+`gammaz'+`deltaz'
local denom = -_b[x]+`gammax'+`deltax'
local theoryrat = `num'/`denom'



display "NUM: `num'"
display "DENOM: `denom'"
display "THEORY RATIO: `theoryrat'"

keep if _n == 1
keep chosen
gen dgp = `dgp'
gen J = `J'
gen naivelogit = `naivelogit'
gen theoryrat = `theoryrat'

drop chosen



